package ch01.thread;

/**
 * Created by zc on 2016/11/7.
 */
public class ProductConsume2 {

    public static void main(String[] args){
        Info2 info=new Info2();
        Producer02 pro=new Producer02(info);
        Consumer02 con=new Consumer02(info);
        new Thread(pro).start();
        new Thread(con).start();
    }
}

class Producer02 implements Runnable{
    private Info2 info=null;
    public Producer02(Info2 info){
        this.info=info;
    }
    public void run(){
        boolean flag=false;
        for(int i=0;i<10;++i)
        {
            if(flag){
                this.info.set("Name A","Content A");

                flag=false;
            }else{
                this.info.set("Name B","Content B");

                flag=true;
            }
        }
    }
};
class Consumer02 implements Runnable{
    private Info2 info=null;
    public Consumer02(Info2 info){
        this.info=info;
    }
    public void run(){
        for(int i=0;i<10;i++)
        {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.info.get();
        }
    }
};

class Info2 {
    private String name;
    private String content;

    public synchronized void get() {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(this.name+"---饰演--->"+this.content);
    }

    public synchronized void set(String name,String content) {
        this.name=name;
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.content = content;
    }
}